package com.imooc.core.mvc;

import com.imooc.core.utils.RequestUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 扩展spring的FreemarkerView，加上base属性。
 * 
 * 支持jsp标签，Application、Session、Request、RequestParameters属性
 * 
 * @author liufang
 * 
 */
public class RichFreeMarkerView extends FreeMarkerView {
	/**
	 * 部署路径属性名称
	 */
	public static final String CONTEXT_PATH = "base";
	private static final Object CONTEXT_IMGPATH = "imgUrl";

	/**
	 * 在model中增加部署路径base，方便处理部署路径问题。
	 */
	@SuppressWarnings("unchecked")
	protected void exposeHelpers(Map model, HttpServletRequest request) throws Exception {
		super.exposeHelpers(model, request);
		model.put(CONTEXT_PATH, RequestUtils.getWebappsDomain(request));
//		model.put(CONTEXT_PATH, "http://127.0.0.1:8080/Sugar");
		// request.setAttribute("base", request.getContextPath());
		String path = request.getContextPath();

		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
		model.put(CONTEXT_IMGPATH, basePath);
	}
}